package com.newhero.product.module.sys.controller;


import java.util.Arrays;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.newhero.product.common.controller.BaseController;
import com.newhero.product.common.mybatis.mapper.EntityWrapper;
import com.newhero.product.common.mybatis.plugins.Page;
import com.newhero.product.common.util.BeanUtils;
import com.newhero.product.common.util.StringUtils;
import com.newhero.product.module.sys.model.Config;
import com.newhero.product.module.sys.service.IConfigService;

/**
 * @ClassName: ConfigController 
 * @Description: 配置管理控制类 
 * @author ymq 
 * @date 2017-04-17 上午14:18
 */
@Controller
@RequestMapping("/admin")
public class ConfigController extends BaseController {

	@Autowired 
	private IConfigService configServiceImpl;

	@RequiresPermissions("/admin/config") 
	@RequestMapping(value = "/config", method = { RequestMethod.GET, RequestMethod.POST })
	public String config(
			@RequestParam(value = "page", defaultValue = "1", required = true) int page,// 页号
			@RequestParam(value = "rows", defaultValue = "" + _ROWS_PER_PAGE, required = true) int rows,// 每页显示的记录条数
			@RequestParam(value = "configname", defaultValue = "", required = false) String configname,
			HttpServletRequest request, HttpServletResponse response, ModelMap model) {
		Page<Config> p = new Page<Config>();
		p.setCurrent(page);
		p.setSize(rows);
		p.setOrderByField("configid");
		EntityWrapper<Config> entityWrapper = new EntityWrapper<Config>();
		if(StringUtils.isNotBlank(configname)){
			entityWrapper.like("configname", configname);
		}
		p = configServiceImpl.selectPage(p,entityWrapper);
		model.addAttribute("data", p);
		model.addAttribute("configname", configname);
		return "sys/config/config";
	}

	@RequestMapping(value = "/configForm", method = RequestMethod.GET)
	public String configForm(String id, HttpServletRequest request) {
		if (id != null && !"".equals(id)) {
			request.setAttribute("configid", id);
			request.setAttribute("formData", configServiceImpl.selectById(id));
		} else {
			request.setAttribute("configid", "");
		}
		return "sys/config/configForm";
	}

	@RequestMapping(value = "/configSave", method = RequestMethod.POST)
	public String configSave(String configid, Config config, HttpServletRequest request,
			HttpServletResponse response, ModelMap model) {
		try {
			if (!StringUtils.isBlank(configid)) {
				Config configold = configServiceImpl.selectById(configid);
				BeanUtils.copy(config, configold);
				model.addAttribute("flag", "数据修改成功！");
				configServiceImpl.updateById(configold);
			} else {
				model.addAttribute("flag", "数据添加成功！");
				configServiceImpl.insert(config);
			}

		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e);
		}
		return config(1, _ROWS_PER_PAGE, "", request, response, model);
	}

	@RequestMapping(value = "/configEdit", method = RequestMethod.POST)
	@ResponseBody
	public Config configEdit(@RequestParam(value = "id", required = true) String configid,
			HttpServletRequest request, HttpServletResponse response) {
		Config config = configServiceImpl.selectById(configid);
		return config;
	}

	@RequestMapping(value = "/configDelete", method = { RequestMethod.POST, RequestMethod.GET })
	public String configDelete(@RequestParam(value = "ids", required = true) String ids,
			HttpServletRequest request, HttpServletResponse response, ModelMap model) {
		String[] idArr = ids.split(",");
		configServiceImpl.deleteBatchIds(Arrays.asList(idArr));
		model.addAttribute("flag", "数据删除成功！");
		return config(1, _ROWS_PER_PAGE, "", request, response, model);
	}
	
	@ResponseBody
	@RequestMapping(value = "/configitemExsit", method = RequestMethod.POST)
	public boolean configitemExsit(
			@RequestParam(value = "configid", defaultValue = "", required = true) String configid,
			@RequestParam(value = "configitem", defaultValue = "", required = true) String configitem,
			HttpServletRequest request, HttpServletResponse response) {
		boolean flag = configServiceImpl.checkConfigitemExsit(configid, configitem);
		System.out.println(flag+"<><><><><>");
		return flag;
	}
	
}
